"use strict";

var _toConsumableArray2 = require("babel-runtime/helpers/toConsumableArray");

var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);

var _keys = require("babel-runtime/core-js/object/keys");

var _keys2 = _interopRequireDefault(_keys);

var _commander = require("commander");

var _commander2 = _interopRequireDefault(_commander);

var _constants = require("./utils/constants");

var _index = require("./index");

var _index2 = _interopRequireDefault(_index);

var _chalk = require("chalk");

var _chalk2 = _interopRequireDefault(_chalk);

var _fs = require("fs");

var _fs2 = _interopRequireDefault(_fs);

var _path = require("path");

var _path2 = _interopRequireDefault(_path);

var _projectConfig = require("../project.config.json");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/* 写出rc文件 */
var rcContent = "";

/* 读入project.config.json中的配置 */


/* 引入中断命令解析器函数 */
// 终端执行工具

for (var key in _projectConfig.rcFile.default) {
  rcContent += key + "=" + _projectConfig.rcFile.default[key] + "\n";
}
_fs2.default.writeFileSync(_path2.default.join(_constants.HOME, _projectConfig.rcFile.name), rcContent);
console.log("===== rcFile completed =====");

/**
 * 定义命令速查表
 * sto commands
 * - config
 * - init
 */
var actionMap = {

  /* init命令配置 */
  init: {
    // init命令描述 终端：sto
    description: "generate a new project from a template",

    // init命令用法提示
    usages: [_projectConfig.cmdName + " init templateName projectName"]
  },

  /* config命令配置 */
  config: {
    // config别名
    alias: "cfg",

    // config 命令描述
    description: "config " + _projectConfig.rcFile.name,

    // config 用法提示
    usages: [_projectConfig.cmdName + " config set <k> <v>", _projectConfig.cmdName + " config get <k>", _projectConfig.cmdName + " config remove <k>"]
  }

  //other commands
};

/* 派发命令给指定的处理器函数 */
(0, _keys2.default)(actionMap).forEach(function (action) {

  _commander2.default.command(action) //接收终端命令 init/config
  .description(actionMap[action].description) //添加命令描述
  .alias(actionMap[action].alias) //添加别名

  /* 定义命令的执行逻辑 */
  .action(function () {
    switch (action) {

      case "config":
        /* config真正的命令执行逻辑 */
        // sto config set key value
        // apply("config",key,value)
        _index2.default.apply(undefined, [action].concat((0, _toConsumableArray3.default)(process.argv.slice(3))));
        break;

      case "init":
        /* init真正的命令执行逻辑 */
        // sto init templateName projectName
        // apply("init",templateName projectName)
        _index2.default.apply(undefined, [action].concat((0, _toConsumableArray3.default)(process.argv.slice(3))));
        break;

      default:
        break;
    }
  });
});

/* sto --help */
function help() {
  console.log("\r\nUsage:");
  (0, _keys2.default)(actionMap).forEach(function (action) {
    actionMap[action].usages.forEach(function (usage) {
      console.log("  - " + usage);
    });
  });
  console.log("\r");
}

/* 定义命令的帮助信息 */
_commander2.default.usage("<command> [options]");

// sto -h 或 sto --help
_commander2.default.on("-h", help);
_commander2.default.on("--help", help);

// sto --version
_commander2.default.version(_constants.VERSION, "-V --version").parse(process.argv);

// sto init
// sto 不带参数时 对外输出帮助信息+文字绿色
if (!process.argv.slice(2).length) {
  _commander2.default.outputHelp(make_green);
}

/* 帮助信息绿色显示 */
function make_green(txt) {
  return _chalk2.default.green(txt);
}